{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "943803af",
   "metadata": {},
   "source": [
    "# 流程控制\n",
    "\n",
    "```{admonition} 需求\n",
    "学习使用 if、while、for 语句。\n",
    "```\n",
    "\n",
    "```{hint}\n",
    "{guilabel}`视频`\n",
    "\n",
    "- [C++ 之 真值判断与 if 语句](https://www.bilibili.com/video/BV15a41167d1)\n",
    "- [C++ 编写循环语句：while、for](https://www.bilibili.com/video/BV1DP4y1H7zR)\n",
    "```\n",
    "\n",
    "C++ 不仅仅可以按照顺序执行指令，还支持流程控制：循环和选择。\n",
    "\n",
    "## 选择语句\n",
    "\n",
    "我们常常需要做选择题。\n",
    "\n",
    "## if 语句\n",
    "\n",
    "我们可以定义分段数学表达式：\n",
    "\n",
    "$$\n",
    "y = \\begin{cases}\n",
    "x * 7 & x < 0\\\\\n",
    "x + 10 & x >= 0\n",
    "\\end{cases}\n",
    "$$\n",
    "\n",
    "首先，初始化变量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aebf0aee",
   "metadata": {},
   "outputs": [],
   "source": [
    "double x {0}, y {0};"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1679d981",
   "metadata": {},
   "source": [
    "给定 `x` 的值，求取 `y` 的值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0decd90e",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = - 4; // x 赋值为 负数\n",
    "if (x < 0) // 条件\n",
    "    y = x *7; // 当 if 条件满足，则执行此赋值语句\n",
    "else\n",
    "    y = x  + 10; // 当 if 条件为假，则执行此赋值语句"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84cc2ec8",
   "metadata": {},
   "source": [
    "可以查看 `y` 的值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f6e947eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-28.000000"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y // 符合预期 x * 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "88748d34",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 5; // x 赋值为 非负数\n",
    "if (x < 0) // 不满足条件\n",
    "    y = x *7;\n",
    "else  // 满足条件\n",
    "    y = x  + 10; // 当 if 条件为假，则执行此赋值语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4eadf1cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15.000000"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y // 符合预期 x + 10"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c21a6d98",
   "metadata": {},
   "source": [
    "下面的分段数学表达式：\n",
    "\n",
    "$$\n",
    "y = \\begin{cases}\n",
    "x * 7 & x < 0\\\\\n",
    "x + 100 & x = 0\\\\\n",
    "x + 10 & x > 0 \n",
    "\\end{cases}\n",
    "$$\n",
    "\n",
    "使用 `if-else` 模式不够用了，需要换成 `if-else if-else` 模式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0a588f45",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 0;\n",
    "if (x < 0)\n",
    "    y = x *7;\n",
    "else if (x == 0)\n",
    "    y = x + 100;\n",
    "else\n",
    "    y = x  + 10; // 当 if 条件为假，则执行此赋值语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ac5e87a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100.00000"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y // 符合预期 x + 100"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef4ff38f",
   "metadata": {},
   "source": [
    "如果有更多的条件分支，可以添加 `else if` 语句。\n",
    "\n",
    "## 循环\n",
    "\n",
    "我们可以需要做一些重复性的工作，在 C++ 中反复执行一段代码的行为称为 **循环** （repetition）或者 **迭代** （iteration）。\n",
    "\n",
    "### while 语句\n",
    "\n",
    "求解数列的和：\n",
    "\n",
    "$$\n",
    "S = \\sum_{x=1}^{10} x^2\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a208acd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "int S {0}; // 初始化 S 用于存储求和的结果\n",
    "int x {0}; // 初始化 x 为 0\n",
    "while (x <= 10) {\n",
    "    S += x;\n",
    "    ++x;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6b8d99d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "55"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S // 符合预期"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7d1aab5",
   "metadata": {},
   "source": [
    "```{hint}\n",
    "`while` 的循环体一定要使用 `{}` 包裹。\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53b47c69",
   "metadata": {},
   "source": [
    "### for 语句\n",
    "\n",
    "上面的数学求和也可以写作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3f3ffd7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "int S {0}; // 初始化 S 用于存储求和的结果\n",
    "for (int x {0}; x <= 10; ++x)\n",
    "    S += x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d47aec97",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "55"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S // 符合预期"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "016a4bc2",
   "metadata": {},
   "source": [
    "## 关于运算符 `==`\n",
    "\n",
    "1. 由运算符 `==` 组成的表达式一定要使用 `()` 起来包裹起来。（避免出现歧义）。\n",
    "2. `(x == y)` 可以理解为隐式类型转换。\n",
    "3. 可以使用强制类型转换 `{x == y}` 表示真值判断表达式。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89d0aea7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "00edc830b0681031d502e33386e4eeda2812b45297ce8c940d13c1f09fed4785"
  },
  "kernelspec": {
   "display_name": "C++11",
   "language": "C++11",
   "name": "python3100jvsc74a57bd0a2954483fe4136a94bd721da8601251ae4c667fedb6adcfe70ff0183b19bacbc"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "C++11",
   "version": "11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
